//
//  MCTableViewAutoFill.m
//  MirkoLibTest
//
//  Created by Mirko on 2017/3/31.
//  Copyright © 2017年 Mirko. All rights reserved.
//

#import "MCTableViewAutoFill.h"
#import <objc/runtime.h>
#import <objc/message.h>

static const NSString *kObjectAssociatedKey = @"object_associated_key";

@implementation MCTableViewAutoFill

-(instancetype) initWithController:(UIViewController *) controller
{
    self = [super initWithController:controller];
    if (self) {
        NSLog(@"start");
        Method sys = class_getInstanceMethod([controller class], @selector(tableView:cellForRowAtIndexPath:));
        Method own = class_getInstanceMethod([self class], @selector(mc_tableView:cellForRowAtIndexPath:));
        class_addMethod([controller class], @selector(mc_tableView:cellForRowAtIndexPath:), method_getImplementation(own), method_getTypeEncoding(own));
        Method newOwn = class_getInstanceMethod([controller class], @selector(mc_tableView:cellForRowAtIndexPath:));
        method_exchangeImplementations(sys, newOwn);
        objc_setAssociatedObject(controller, &kObjectAssociatedKey, self, OBJC_ASSOCIATION_ASSIGN);
        NSLog(@"end");
        NSLog(@"");
    }
    return self;
}

-(NSArray *) findValidView:(UITableViewCell *) cell
{
    NSMutableArray *arr = [self listSubviewsOfView:cell.contentView rootView:cell.contentView];
    return [self sortViewList:arr rootView:cell.contentView];
}

- (UITableViewCell *) mc_tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Cell row :%ld", indexPath.row);
    UITableViewCell *cell = [tableView.dataSource performSelector:@selector(mc_tableView:cellForRowAtIndexPath:) withObject:tableView withObject:indexPath];
    
    MCTableViewAutoFill *autoFill = objc_getAssociatedObject(self, &kObjectAssociatedKey);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSArray *fillData = autoFill.autoFillDatas[indexPath.row];
        NSArray *cellViews = [autoFill findValidViewWithRootView:cell.contentView];
        int counter = MIN((int)fillData, (int)(cellViews.count));
        for (int i = 0; i <counter; ++i) {
            UIView *view = cellViews[i];
            NSString *string = fillData[i];
            dispatch_async(dispatch_get_main_queue(), ^{
                [view autoFill:string];
            });
        }
    });
    return cell;
}

-(void) removeAutoFill
{
    Method sys = class_getInstanceMethod([self.viewController class], @selector(tableView:cellForRowAtIndexPath:));
    Method own = class_getInstanceMethod([self.viewController class], @selector(mc_tableView:cellForRowAtIndexPath:));
    method_exchangeImplementations(sys, own);
    objc_setAssociatedObject(self.viewController, &kObjectAssociatedKey, nil, OBJC_ASSOCIATION_ASSIGN);
}

//-(void) dealloc
//{
//    Method sys = class_getInstanceMethod([self class], @selector(tableView:cellForRowAtIndexPath:));
//    Method own = class_getInstanceMethod([self.viewController class], @selector(mc_tableView:cellForRowAtIndexPath:));
//    method_exchangeImplementations(sys, own);
//    objc_setAssociatedObject(self.viewController, &kObjectAssociatedKey, nil, OBJC_ASSOCIATION_ASSIGN);
//}

@end
